<?php
/**
* 名称：通用上传控制器。
* 描述：应用上传图片通过该程式统一完成。
*/
final class Controller_Common_Upload extends Controller_Public_Account{
    /**
    * 上传路径标量。
    * @var     bool
    * @default TRUE
    * @access  private
    */
    protected $Path=array(
        'news'=>'/data/000/news',
        'slide'=>'/data/000/slide',
        'ad'=>'/data/000/ad',
        'exhibition'=>'/data/000/exhibition'
    );

    /**
    * 描述：统一上传视图。
    * @access protected
    * @return void
    */
    protected function Main(){
        $temp=explode('|',post('data'));
        //~
        $data=array(
            'number'=>randomNumber()+post('number'),
            'sourceControlId'=>post('sourceControlId'),
            'type'=>$temp[1],
            'path'=>str_replace('/','\\',$this->Path[$temp[1]]),
            'errorPanelId'=>$temp[0],
        );
        //~
        self::$Template->Assign('data',$data);
        self::$Template->Display('oui/common/upload/view.html');
    }

    /**
    * 描述：文件上传程式。
    * @access protected
    * @return void
    */
    protected function Execute(){
        $data=post('x-upload-data');
        $temp=explode(',',$data);
        //~
        $var=array(
            'error'=>NULL,
            'filename'=>NULL,
            'fileControlName'=>$temp[2],
            'path'=>$this->Path[$temp[4]] //$temp[4]等于type。
        );
        //~
        $ups=new Uploader($var['fileControlName']);
        $ups->Type=Uploader::UPLOADTYPE_SINGLE;
        $ups->Size=1024*1000;
        $ups->Extension=explode(',',Uploader::UPLOAD_EXT_IMAGE);
        $ups->BasePath=BUILD_ROOT;
        $ups->LocatePath=$var['path'];
        $ups->Execute();
        switch($ups->ErrorCode){
            case Uploader::UPLOAD_ERR_OK:
                $var['filename']=$ups->Filename;
            break;
            case Uploader::UPLOAD_ERR_NO_FILE:
                $error='请上传文件。';
            break;
            case Uploader::UPLOAD_ERR_CUSTOM_EXTENSION:
                $error='文件类型不正确，只能是'.join('、',explode(',',Uploader::UPLOAD_EXT_IMAGE)).'格式。';
            break;
            case Uploader::UPLOAD_ERR_CUSTOM_SIZE:
                $error='文件大小超过规定，不能超过'.Uploader::SizeFormat($ups->Size).'。';
            break;
            case Uploader::UPLOAD_ERR_CUSTOM_NOFIELD:
                $error='发生异常，请稍候再试！~。';
            break;
            default:
                $error='上传失败，请稍候再试！';
            break;
        }
        //echo  '<hr/>',$ups->PrintDebugInfo();
        self::$Template->Assign('data',$data);
        self::$Template->Assign('error',$var['error']);
        self::$Template->Assign('filename',$var['filename']);
        self::$Template->Display('oui/common/upload/execute.html');
    }
}